// pages/wallet/recharge.js
var app = getApp();
var util = require('../../utils/util.js');
var inputContent = {};//输入内容

Page({

    /**
     * 页面的初始数据
     */
    data: {
        form: {},
        axis: [],
        isDisabled: false,
        msg: "",
        productIdent: "",
        imgShow: true,
        imgShow1: true,
        loginIdShow: true,
        region: [],
        customItem: [],
        city: "",
        area: "",
        province: "",
        cityTwo: "",
        areaTwo: "",
        provinceTwo: "",
        addHouseShow: false,
        addHouseName: "添加第二套抵押物地址"
    },
    /**
     * 生命周期函数--监听页面加载
     */
    // bindRegionChange: function (e) {
    //     console.log(e)
    //     this.setData({
    //         'form.basicBorrowerMateAddress': e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    //     })
    // },
    // bindRegionChange1: function (e) {
    //     console.log(e)
    //     this.setData({
    //         'form.basicBorrowerAddress': e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    //     })
    // },
    //省市联动
    // bindRegionChangeOne: function (e) {
    //     var that = this
    //     //为了让选择框有个默认值，    
    //     that.setData({
    //         clas: ''
    //     })
    //     this.setData({
    //         //拼的字符串传后台
    //         detailed: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
    //         //下拉框选中的值
    //         // region: e.detail.value,
    //         province: e.detail.code[0],
    //         city: e.detail.code[1],
    //         area: e.detail.code[2],
    //     })
    //     console.log('picker发送选择改变，携带值为', e)
    //     console.log(that.data.province, that.data.city, that.data.area)
    // },
    // bindRegionChangeTwo: function (e) {
    //     var that = this
    //     //为了让选择框有个默认值，    
    //     that.setData({
    //         clasTwo: ''
    //     })
    //     this.setData({
    //         //拼的字符串传后台
    //         detailedTwo: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
    //         //下拉框选中的值
    //         // region: e.detail.value,
    //         provinceTwo: e.detail.code[0],
    //         cityTwo: e.detail.code[1],
    //         areaTwo: e.detail.code[2],
    //     })
    // },
    onLoad: function (options) {
        let that = this
        let orderNumber = wx.getStorageSync("orderNumber")
        let belongId = wx.getStorageSync("belongId")
        let loginId = wx.getStorageSync("loginId")
        that.setData({
            orderNumber: orderNumber,
            belongId: belongId
        })
        that.getIndexData(orderNumber)
        that.findList(orderNumber)
        if (belongId != loginId) {
            that.setData({
                isDisabled: true,
                loginIdShow: false
            })
        }
    },
    // 分享
    onShareAppMessage: function (res) {
        var that = this;
        return {
            title: '贷投狗小程序',
            path: 'pages/wallet/recharge',
            success: function (res) {
                that.shareClick();
            },
            fail: function (res) {
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    msg: function (e) {
        this.setData({
            msg: e.detail.value
        })
    },
    formSubmit: function () {
        console.log(this.data.addHouseShow,"show")
        let data = {}
        data.form = this.data.form
        data.orderNumber = this.data.form.orderNumber
        data.msg = this.data.msg
        console.log(data)
        let card = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
        if (this.data.productIdent == 1) {
            if (data.form.basicBorrowerCard && data.form.basicBorrowerName && data.form.basicBorrowerPhone) {
                if (!card.test(data.form.basicBorrowerCard)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人身份证号',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                if (!/^1[3456789]\d{9}$/.test(data.form.basicBorrowerPhone)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人联系电话',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                this.submit(data)
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: '请完成信息录入',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        } else {
            if (data.form.basicBorrowerCard && data.form.basicBorrowerName && data.form.basicBorrowerPhone && data.form.basicLoansMoney && data.form.pledgeHouseAddress) {
                if (!card.test(data.form.basicBorrowerCard)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人身份证号',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                if (!/^1[3456789]\d{9}$/.test(data.form.basicBorrowerPhone)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人联系电话',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                this.submit(data)
            } else {
                console.log("成功")
                wx.showModal({
                    title: '温馨提示',
                    content: '请完成信息录入',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        }
        
    },
    submit: function (data) {
        util.https(app.globalData.api + "/applet/order/updateFrom", "PUT", data,
        function (data) {
            if (data.code == 200) {
                wx.showModal({
                    title: '提示',
                    content: data.message,
                    showCancel: false,//是否显示取消按钮
                    confirmText: "确定",
                    success: function (res) {
                        if (res.cancel) {
                        } else {
                            wx.navigateBack({
                                delta: 1,
                                success: function (e) {
                                    var page = getCurrentPages().pop();
                                    if (page == undefined || page == null) return;
                                    page.onLoad();
                                }
                            })
                        }
                    }
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: data.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) { }
                    }
                })
            }
        }
    )
    },
    onShow: function() {
        this.setData({
            addHouseShow:false
        })
    },

    /**
     * 获取用户输入
     */
    bindChange: function (e) {
        inputContent[e.currentTarget.id] = e.detail.value;
    },
    // 获取数据
    getIndexData: function (orderNumber) {
        var that = this;
        // 反馈信息
        util.https(app.globalData.api + "/applet/home/findByDictCode/" + "payment_status", "GET", {},
            function (data) {
                if (data.code == 200) {
                    console.log(data)
                    that.setData({
                        paymentStatus: data.data
                    })
                }
            })
        util.https(app.globalData.api + "/applet/order/data/" + orderNumber, "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        orderData: data.data,
                        form: data.data.form,
                        productIdent: data.data.productMode.minProcedureCode,
                        orderType: data.data.productMode.productName
                    })
                } else {
                    wx.showModal({
                        title: '警告',
                        content: data.message,
                        showCancel: false,
                        confirmText: '确定',
                    })
                }
            }
        )
    },
    // 操作记录
    findList: function (orderNumber) {
        var that = this;
        util.https(app.globalData.api + "/applet/order/findList/" + orderNumber, "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        axis: data.data
                    })
                } else {
                    wx.showModal({
                        title: '警告',
                        content: data.message,
                        showCancel: false,
                        confirmText: '确定',
                    })
                }
            }
        )
    },
    inputText: function (e) {
        let dataset = e.currentTarget.dataset;
        let value = e.detail.value;
        this.data[dataset.obj][dataset.item] = value;
        this.setData({
            form: this.data[dataset.obj]
        })
    },
})